// pages/shop/shop.js
const {getcartdataApi,getremovecartApi,getchangeselectApi,getcartnumApi} =require('../../http/api')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 购物车数据
    cartdata:[],
    // checked: true,
    // 总计合计
     picss:0
  },
  
  // 封装商品合计函数
    picnum (){
      let arr = this.data.cartdata.filter(item=>{
        return item.selected==true
      })
      // console.log(arr);
       let pics = 0
       if(arr.length ==0 ){
        this.setData({
          picss:0
        })
       }else{
        arr.forEach(item=>{
          pics += (item.number * item.price) * 100
          // console.log('aa',pics);
          this.setData({
            picss:pics
          })
        })
       }

    },
    //  点击增加数量
    addnum(event){
        // console.log(event);
        console.log('obj1',event.currentTarget.dataset.item.number);
        
        let obj1 = {
          key:event.currentTarget.dataset.item.key,
          number:event.currentTarget.dataset.item.number+1,
          token:wx.getStorageSync('token')
        }
        console.log('obj',event.currentTarget.dataset.item.number);
        getcartnumApi(obj1).then(res=>{
           console.log('res',res.data.items);
           this.picnum()

           getcartdataApi(wx.getStorageSync('token')).then(res=>{
            console.log('1212',res);
            this.setData({
              cartdata:res.data.items
            })
            // console.log('11',this.cartdata);
            // picnum ()
            this.picnum()
      
            // console.log(this.data.cartdata);
          })
        })
        
        
    },
    // 减少数量
    jiannum(event){
      let obj = {
        key:event.currentTarget.dataset.item.key,
        number:event.currentTarget.dataset.item.number-1,
        token:wx.getStorageSync('token')
      }
      getcartnumApi(obj).then(res=>{
         console.log(res);
         this.picnum()
        //  重新渲染数据
         getcartdataApi(wx.getStorageSync('token')).then(res=>{
          // console.log(res);
          this.setData({
            cartdata:res.data.items
          })
          // picnum ()
          this.picnum()
    
          // console.log(this.data.cartdata);
        })

      })
      
      
    },
    // 点击改变选中状态
  onChange(event) {
    let obj = {
      key:event.currentTarget.dataset.item.key,
      selected:!event.currentTarget.dataset.item.selected,
      token:wx.getStorageSync('token')
    }
    getchangeselectApi(obj).then(res=>{
        //  console.log(res);
         getcartdataApi(wx.getStorageSync('token')).then(res=>{
          // console.log(res);
          this.setData({
            cartdata:res.data.items
          })
          // console.log(this.data.cartdata);
          this.picnum()
        })
        
    })
  },

  // 点击删除商品
  remove(e){
    // console.log(e.currentTarget.dataset.key);
    let obj = {
      key:e.currentTarget.dataset.key,
      token:wx.getStorageSync('token')
    }
    // console.log(obj);
    getremovecartApi(obj).then(res=>{
      // console.log(res);
      // Toast.success('删除成功');
      getcartdataApi(wx.getStorageSync('token')).then(res=>{
        // console.log(res);
        this.setData({
          cartdata:res.data.items
        })
        // picnum ()
        this.picnum()
        // console.log(this.data.cartdata);
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getcartdataApi(wx.getStorageSync('token')).then(res=>{
      // console.log(res);
      this.setData({
        cartdata:res.data.items
      })
      // picnum ()
      this.picnum()

      // console.log(this.data.cartdata);
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})